package org.plex.core.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.plex.core.TaskManager;
import org.plex.model.TaskACL;
import org.plex.task.CompleteTaskResult;
import org.plex.task.CreateTaskInfo;
import org.plex.task.CreateTaskResult;
import org.plex.task.FormInstance;
import org.plex.task.TaskInfo;
import org.plex.task.TaskUserinfo;
import org.plex.task.imp.CompleteTaskResultImp;
import org.plex.task.imp.CreateTaskInfoImp;
import org.plex.task.imp.CreateTaskResultImp;
import org.plex.task.imp.FormInstanceImp;
import org.plex.task.imp.TaskInfoImp;
import org.plex.task.imp.TaskUserInfoImp;

import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
import com.adobe.idp.taskmanager.dsc.client.TaskManagerClientFactory;
import com.adobe.idp.taskmanager.dsc.client.task.TaskACLImpl;
import com.adobe.idp.taskmanager.dsc.client.task.TaskManagerException;
import com.adobe.idp.taskmanager.dsc.client.task.TaskUserInfo;

public class TaskManagerImp implements TaskManager
{

	private ServiceClientFactory									serviceClientFactory;

	private com.adobe.idp.taskmanager.dsc.client.task.TaskManager	taskManager;

	public TaskManagerImp(ServiceClientFactory serviceClientFactory)
			throws NullPointerException
	{
		if (null == serviceClientFactory)
		{
			throw new NullPointerException();
		}
		this.serviceClientFactory = serviceClientFactory;
		this.taskManager = TaskManagerClientFactory
				.getTaskManager(serviceClientFactory);
	}

	public void cancelTask(long taskId) throws TaskManagerException
	{
		this.taskManager.cancelTask(taskId);
	}

	public void changeQueueForTask(long taskId, long targetQueueId)
			throws TaskManagerException
	{
		this.taskManager.changeQueueForTask(taskId, targetQueueId);

	}

	public void claimTask(long taskId) throws TaskManagerException
	{
		this.taskManager.claimTask(taskId);

	}

	public CompleteTaskResult completeTask(long taskId)
			throws TaskManagerException
	{

		return new CompleteTaskResultImp(this.taskManager.completeTask(taskId));
	}

	public CompleteTaskResult completeTask(long taskId, String selectedAction)
			throws TaskManagerException
	{
		return new CompleteTaskResultImp(this.taskManager.completeTask(taskId,
				selectedAction));
	}

	@Deprecated
	public CompleteTaskResult completeTask(long taskId,
			FormInstance formInstance) throws TaskManagerException
	{
		FormInstanceImp imp = (FormInstanceImp) formInstance;
		return new CompleteTaskResultImp(this.taskManager.completeTask(taskId,
				imp.getFormInstance()));
	}

	public void consultTask(long taskId, String userOidToConsult)
			throws TaskManagerException
	{
		this.taskManager.consultTask(taskId, userOidToConsult);
	}

	@Deprecated
	public CreateTaskResult createTask(long actionId, FormInstance formdata,
			String instructions, long queueId, boolean copyAttachments,
			boolean showAttachments) throws TaskManagerException
	{
		FormInstanceImp imp = (FormInstanceImp) formdata;
		return new CreateTaskResultImp(this.taskManager.createTask(actionId,
				imp.getFormInstance(), instructions, queueId, copyAttachments,
				showAttachments));
	}

	@Deprecated
	public CreateTaskResult createTask(long actionId, FormInstance formdata,
			String instructions, long queueId, List attachmentsToCopy,
			boolean showAttachements) throws TaskManagerException
	{
		// TODO Auto-generated method stub
		FormInstanceImp imp = (FormInstanceImp) formdata;
		return new CreateTaskResultImp(taskManager.createTask(actionId, imp
				.getFormInstance(), instructions, queueId, attachmentsToCopy,
				showAttachements));
	}

	@Deprecated
	public CreateTaskResult createTask(FormInstance formdata,
			String instructions, boolean showAttachements)
			throws NullPointerException, TaskManagerException
	{
		// TODO Auto-generated method stub
		FormInstanceImp imp = (FormInstanceImp) formdata;
		return new CreateTaskResultImp(taskManager.createTask(imp
				.getFormInstance(), instructions, showAttachements));
	}

	public CompleteTaskResult deadlineTask(long taskId, String deadlineAction,
			boolean changeInstructions, String deadlineInstructions)
			throws TaskManagerException
	{
		// TODO Auto-generated method stub
		return new CompleteTaskResultImp(taskManager.deadlineTask(taskId,
				deadlineAction, changeInstructions, deadlineInstructions));
	}

	public void escalateTask(long taskId, long queueId) throws TaskManagerException
	{
		this.taskManager.escalateTask(taskId, queueId);
	}

	public void forwardTask(long taskId, String userIoForward)
			throws TaskManagerException
	{
		this.taskManager.forwardTask(taskId, userIoForward);

	}

	public void reAssignTask(long taskId, String reAssignToUserOid)
			throws TaskManagerException
	{
		this.taskManager.reAssignTask(taskId,reAssignToUserOid);
	}

	public void rejectTask(long taskId) throws TaskManagerException
	{
		this.taskManager.rejectTask(taskId);
	}

	public void sendTaskReminder(long taskId, boolean changeInstructions,
			String reminderInstructions, long nextReminder)
			throws TaskManagerException
	{
		this.taskManager.sendTaskReminder(taskId,changeInstructions, reminderInstructions, nextReminder);
	}

	public void sendTaskReminder(long taskId, boolean changInstructions,
			String reminderInstructions, long nextReminder,
			boolean userBusinessDays) throws TaskManagerException
	{
	  this.taskManager.sendTaskReminder(taskId, changInstructions, reminderInstructions, nextReminder,userBusinessDays);

	}

	public void shareTask(long taskId, String userOidToShare)
			throws TaskManagerException
	{
	    this.taskManager.shareTask(taskId, userOidToShare);
	}

	public CompleteTaskResult terminateTask(long taskId)
			throws TaskManagerException
	{
		
		return new CompleteTaskResultImp(this.taskManager.terminateTask(taskId));
	}



	public void setServiceClientFactory(
			ServiceClientFactory serviceClientFactory)
			throws NullPointerException
	{
		if (null == serviceClientFactory)
		{
			throw new NullPointerException();
		}
		this.serviceClientFactory = serviceClientFactory;
		this.taskManager = TaskManagerClientFactory
				.getTaskManager(serviceClientFactory);
	}

	public ServiceClientFactory getServiceClientFactory()
	{
		return serviceClientFactory;
	}

	public CreateTaskResult createTask(CreateTaskInfo createTaskInfo)
			throws TaskManagerException
	{
		CreateTaskInfoImp imp=(CreateTaskInfoImp)createTaskInfo;
		return new CreateTaskResultImp(taskManager.createTask(imp.getCreateTaskInfo()));
	}

	public List<CompleteTaskResult> terminateTasksForJobId(String jobId)
			throws TaskManagerException
	{
	    List<CompleteTaskResult> list=new ArrayList<CompleteTaskResult>();
	    com.adobe.idp.taskmanager.dsc.client.task.CompleteTaskResult[] completeTaskResults=taskManager.terminateTasksForJobId(jobId);
	    for(com.adobe.idp.taskmanager.dsc.client.task.CompleteTaskResult completeTaskResult:completeTaskResults)
	    {
	        list.add(new CompleteTaskResultImp(completeTaskResult));   	
	    }
		return list;
	}

	@Deprecated
	public void changeDeadline(long taskId, Date newDeadline) throws TaskManagerException
	{
		this.taskManager.changeDeadline(taskId,newDeadline);
		
	}

	@Deprecated
	public void changeReminder(long taskId, Date newReminder) throws TaskManagerException
	{
		// TODO Auto-generated method stub
		this.taskManager.changeReminder(taskId, newReminder);
		
	}

	
	public void changedescription(long taskId, String updateDesciption) throws TaskManagerException
	{
		// TODO Auto-generated method stub
		this.taskManager.changeDescription(taskId, updateDesciption);
	}

	public void claimTask(long taskId, boolean locktask)
	{
	    claimTask(taskId, locktask);	
	}

	public TaskInfo getTaskInfo(long taskId) throws NullPointerException, TaskManagerException
	{
		// TODO Auto-generated method stub
		return new TaskInfoImp(taskManager.getTaskInfo(taskId));
	}

	public TaskInfo getTaskInfo(long taskId, boolean returnFormInstance) throws NullPointerException, TaskManagerException
	{
		// TODO Auto-generated method stub
		return new TaskInfoImp(taskManager.getTaskInfo(taskId,returnFormInstance));
	}

	public List<TaskUserinfo> getUsersWithAccessToTask(long taskId) throws TaskManagerException
	{
		TaskUserInfo[] taskUserInfos=taskManager.getUsersWithAccessToTask(taskId);
		List<TaskUserinfo> list=new ArrayList<TaskUserinfo>();
		for(TaskUserInfo info:taskUserInfos)
		{
			list.add(new TaskUserInfoImp(info));
		}
		return list;
	}

	public void lockTask(long taskId) throws TaskManagerException
	{
		this.taskManager.lockTask(taskId);
		
	}

	public void modifyTaskPermissions(long taskId,
			List<? extends TaskACL> taskACLList) throws TaskManagerException
	{
	     int size=taskACLList.size();
	     com.adobe.idp.taskmanager.dsc.client.task.TaskACL[] taskACLs=new TaskACLImpl[size];
	     for(int i=0;i<size;i++)
	     {
	    	 TaskACLImp imp=(TaskACLImp)taskACLList.get(i);
	    	 taskACLs[i]=imp.getTaskACL();
	     }
	     
	    taskManager.modifyTaskPermissions(taskId, taskACLs);
	}

	public void setVisibility(long taskId, boolean visible) throws TaskManagerException
	{
		taskManager.setVisibility(taskId, visible);
		
	}

	public void unLockTask(long taskId) throws TaskManagerException
	{
          taskManager.unLockTask(taskId);
	}

}
